//package com.face.console.tools.filter;
//
//
//import com.face.console.tools.Audience;
//import com.face.console.tools.JwtIgnore;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.BeanFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.HttpMethod;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.support.WebApplicationContextUtils;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.lang.reflect.Method;
//
//@Slf4j
//public class JwtInterceptorFilter extends HandlerInterceptorAdapter{
//
//    @Autowired
//    private Audience audience;
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        // 忽略带JwtIgnore注解的请求, 不做后续token认证校验
////如果请求为静态资源请求时，类型转换会报错，类型不对应，所以应再请求时方法请求时再转换类型
//// 如果不是映射到方法直接通过
//        if (handler instanceof HandlerMethod) {
//            HandlerMethod handlerMethod2 = (HandlerMethod) handler;
//            JwtIgnore jwtIgnore = handlerMethod2.getMethodAnnotation(JwtIgnore.class);
//            if (jwtIgnore != null) {
//                System.out.println("帶jwtIgnore注解 ");
//                return true;
//            }
//        }else if (handler instanceof ResourceHttpRequestHandler){
//            return true;
//        }
////当请求为方法时，再转换类型
//
////        if (handler instanceof HandlerMethod) {
////            HandlerMethod handlerMethod = (HandlerMethod) handler;
////            JwtIgnore jwtIgnore = handlerMethod.getMethodAnnotation(JwtIgnore.class);
////            if (jwtIgnore != null) {
////                System.out.println("1234");
////                return true;
////            }
////        }
//        if (HttpMethod.OPTIONS.equals(request.getMethod())) {
//            response.setStatus(HttpServletResponse.SC_OK);
//            return true;
//        }
//        // 获取请求头信息authorization信息
//        final String authHeader = request.getHeader(JwtTokenUtil.AUTH_HEADER_KEY);
//        log.info("## 获取请求头信息authorization信息 = {}", authHeader);
//        if (StringUtils.isBlank(authHeader) || !authHeader.startsWith(JwtTokenUtil.TOKEN_PREFIX)) {
//            log.info("### 用户未登录，请先登录 ###");
//            throw new RuntimeException("### 用户未登录，请先登录 ###");
//        }
//        // 获取token
//        final String token = authHeader.substring(7);
//        if(audience == null){
//            BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
//            audience = (Audience) factory.getBean("audience");
//        }
//        // 验证token是否有效--无效已做异常抛出，由全局异常处理后返回对应信息
//        JwtTokenUtil.parseJWT(token, audience.getBase64Secret());
//        return true;
//    }
//}
